package com.futures.investm.activity;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.futures.investm.R;
import com.futures.investm.adapter.ExchangeAdapter;
import com.futures.investm.constant.Api;
import com.futures.investm.entity.ExchangeEntity;
import com.aries.ui.view.title.TitleBarView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.mode.CacheMode;

import java.util.ArrayList;

import butterknife.BindView;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.entity.BaseTResp;

public class ExchangeActivity extends BaseTitleActivity {

    @BindView(R.id.rv_contentFastLib)
    RecyclerView recyclerView;

    ExchangeAdapter mAdapter;
    @BindView(R.id.smartLayout_rootFastLib)
    SmartRefreshLayout mSmartLayoutRootFastLib;
    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("交易所排行榜");
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_exchange;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mAdapter = new ExchangeAdapter(R.layout.item_market);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL));
        request();

        mSmartLayoutRootFastLib.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                request();
                mSmartLayoutRootFastLib.finishRefresh();
            }
        });
    }

    private void request() {
        ViseHttp.GET(Api.EXCHANGE)
                .baseUrl(Api.BASE_URL)
                .setHttpCache(true)
                .cacheMode(CacheMode.FIRST_REMOTE)
                .request(new ACallback<BaseTResp<ArrayList<ExchangeEntity>>>() {
                    @Override
                    public void onSuccess(BaseTResp<ArrayList<ExchangeEntity>> datas) {

                        if (1 == datas.code) {
                            mAdapter.replaceData(datas.data);
                        } else {
//                            ToastUtils.showToast("请求数据失败");
                        }

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                    }
                });
        recyclerView.setAdapter(mAdapter);
    }

}
